<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Controller\Admin;

use App\Service\AdminService;
use App\Service\UserService;
use App\Utils\Result;
use App\Middleware\Auth\AdminTokenMiddleware;
use App\Middleware\Auth\AdminPowerMiddleware;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Contract\RequestInterface;
use App\Annotation\AdminNode;

/**
 * @AdminNode(name="用户管理",isMenu=1,sort=30,style="layui-icon-group")
 * @Middlewares({
 *      @Middleware(AdminTokenMiddleware::class),
 *      @Middleware(AdminPowerMiddleware::class)
 * })
 * @AutoController
 */
class UserController
{
    /**
     * @Inject()
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject()
     * @var AdminService
     */
    protected $adminService;

    /**
     * @Inject()
     * @var UserService
     */
    protected $userService;

    /**
     * @AdminNode(name="管理员管理",isMenu="1")
     * 管理员
     * @return mixed
     */
    public function index()
    {
        $param = $this->request->all();
        $where = [];
        if(isset($param['status']) && $param['status']!=''){
            $where[] = ['status','=',$param['status']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['user_name','like','%'.$param['name'].'%'];
        }
        $data = $this->adminService->getUsers($where,'id,user_name,real_name,role_id,avatar,login_times,status,last_login_ip,last_login_time',$param['limit']);
        return Result::success($data);
        return $this->fetch('admin/user/index');
    }

    /**
     * @AdminNode(name="用户管理",isMenu="1")
     *
     * @return mixed
     */
    public function userList()
    {
        $param = $this->request->all();
        $where = [];
        $whereOr = [];
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['user_name','like','%'.$param['name'].'%'];
        }
        if(isset($param['openid']) && !empty($param['openid'])){
            $whereOr[] = ['mp_openid','like',$param['openid'].'%'];
            $whereOr[] = ['official_openid','like',$param['openid'].'%'];
            $whereOr[] = ['app_openid','like',$param['openid'].'%'];
        }

        $res = $this->userService->getUsers($where,$whereOr,'*',10);
        return Result::success($res);
    }

    /**
     * @AdminNode(name="添加管理员")
     * @return mixed
     */
    public function addAdmin()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id']) || !isset($param['password']) || empty($param['password'])){
            return Result::error('参数异常');
        }
        $res = $this->adminService->editAccount($param['id'],$param);
        if($res){
            return Result::success($res);
        }else{
            return Result::error('添加失败');
        }
    }

    /**
     * @AdminNode(name="修改管理员")
     * @return mixed
     */
    public function editAdmin()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->adminService->editAccount($param['id'],$param);
        if($res){
            return Result::success($res);
        }else{
            return Result::error('修改失败');
        }
    }

    /**
     * @AdminNode(name="修改管理员")
     * @return mixed
     */
    public function editPassword()
    {
        $param = $this->request->all();
        $user = $this->adminService->getUser([['user_name','=',$param['user_name']]],'id,user_name');
        if(empty($user)){
            return Result::error('用户不存在');
        }
        $param['id'] = $user['id'];
        $res = $this->adminService->editPwd($param);
        if($res){
            return Result::success($res);
        }else{
            return Result::error('修改失败');
        }
    }

    /**
     * @AdminNode(name="角色列表",isMenu="1")
     * @return mixed
     */
    public function role()
    {
        $param = $this->request->all();
        $where[] = ['id','>',0];
        if(isset($param['name'])){
            $where[] = ['role_name','like','%'.$param['name'].'%'];
        }
        if(isset($param['status'])){
            $where[] = ['status','=',$param['status']];
        }
        $data = $this->adminService->getRoles($where,'*',$param['limit']);
        return Result::success($data);
    }

    /**
     * @AdminNode(name="添加角色")
     *
     * @return mixed
     */
    public function addRole()
    {
        $param = $this->request->all();
        $res = $this->adminService->addRole($param);
        if($res){
            return Result::success($res);
        }else{
            return Result::error('添加失败');
        }
    }

    /**
     * @AdminNode(name="修改角色")
     * @return mixed
     */
    public function editRole()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $id = $param['id'];
        $res = $this->adminService->editRole($id,$param);
        if($res){
            return Result::success($res);
        }else{
            return Result::error('修改失败');
        }
    }

    /**
     * @AdminNode(name="禁用角色")
     *
     * @return mixed
     */
    public function banRole()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $res = $this->adminService->editRole($param['id'],['status'=>$param['status']]);
        if($res){
            for_clear_redis_cache('HyperCMF:role_menu_data*');
            return Result::success($res);
        }else{
            return Result::error('禁用失败');
        }
    }

    /**
     * @AdminNode(name="权限列表")
     *
     * @return mixed
     */
    public function power()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }
        $power = $this->adminService->getPower($param['id']);
        return Result::success(for_get_tree(for_obj_to_array($power), true,true));
    }

    /**
     * @AdminNode(name="设置用户权限")
     *
     * @return mixed
     */
    public function setPower()
    {
        $param = $this->request->all();
        if(!isset($param['id']) || empty($param['id'])){
            return Result::error('参数异常');
        }

        $ids = array_column(json_decode($param['nodes']),'id');
        sort($ids);
        $res = $this->adminService->editRole($param['id'],['rule'=>implode(',',$ids)]);
        if($res){
            $this->adminService->setPower($param);
            for_clear_redis_cache('HyperCMF:role_menu_data*');
            return Result::success($res);
        }else{
            return Result::error('操作失败');
        }
    }

}
